/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.course.entity.CourseChapterSubEntity;
import org.springblade.modules.course.entity.CourseEntity;
import org.springblade.modules.course.service.ICourseChapterSubService;
import org.springblade.modules.course.service.ICourseService;
import org.springblade.modules.course.vo.CourseChapterSubVO;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.course.entity.CourseChapterEntity;
import org.springblade.modules.course.vo.CourseChapterVO;
import org.springblade.modules.course.wrapper.CourseChapterWrapper;
import org.springblade.modules.course.service.ICourseChapterService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 归属某个课程的具体章节列表 控制器
 *
 * @author
 * @since 2023-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-courseChapter/courseChapter")
@Api(value = "课程的具体章节列表", tags = "课程的具体章节列表接口")
public class CourseChapterController extends BladeController {
	private final ICourseService courseService;
	private final ICourseChapterService courseChapterService;
	private final ICourseChapterSubService courseChapterSubService;
	private final CommonUtil commonUtil;

	/**
	 * 归属某个课程的具体章节列表 详情
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入courseChapter")
	public R<CourseChapterVO> detail(CourseChapterEntity courseChapter) {
		CourseChapterEntity detail = courseChapterService.getOne(Condition.getQueryWrapper(courseChapter));
		CourseChapterVO courseChapterVO = CourseChapterWrapper.build().entityVO(detail);

		//设置该章节对应的课程名称
		CourseEntity courseEntity = courseService.getById(detail.getCourseId());
		if (null!=courseEntity)courseChapterVO.setCourseName(courseEntity.getName());

		//设置章节的小节
		List<CourseChapterSubEntity> courseChapterSubEntityList = courseChapterSubService.selectByChapterId(detail.getId());
		courseChapterVO.setChapterSubList(courseChapterSubEntityList.stream().map(courseChapterSubEntity -> {
			CourseChapterSubVO courseChapterSubVO = new CourseChapterSubVO();
			BeanUtil.copyProperties(courseChapterSubEntity, courseChapterSubVO);
			return courseChapterSubVO;
		}).collect(Collectors.toList()));


		return R.data(courseChapterVO);
	}

	/**
	 * 归属某个课程的具体章节列表 自定义分页
	 * 非管理员只能看到自己课程、或者自己创建的章节列表、
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入courseChapter")
	public R<IPage<CourseChapterVO>> page(CourseChapterVO courseChapter, Query query) {
//		if (!commonUtil.checkRoleAlias(TrainingRoleConstant.Administrator)) courseChapter.setCreateUser(AuthUtil.getUserId());
		commonUtil.checkRoleAliasPermission(courseChapter,courseChapter.getClass());
		IPage<CourseChapterVO> pages = courseChapterService.selectCourseChapterPage(Condition.getPage(query), courseChapter);
		pages.getRecords().forEach(record->{
			CourseEntity courseEntity = courseService.getById(record.getCourseId());
			if (null!=courseEntity)record.setCourseName(courseEntity.getName());
		});
		return R.data(pages);
	}

	/**
	 * 归属某个课程的具体章节列表 新增
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.ADD,describe = "课程章节新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入courseChapter")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R save(@Valid @RequestBody CourseChapterVO vo) {
		return R.status(courseChapterService.save(vo));
	}

	/**
	 * 归属某个课程的具体章节列表 修改
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.UPDATE,describe = "课程章节修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入courseChapter")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R update(@Valid @RequestBody CourseChapterEntity courseChapter) {
		return R.status(courseChapterService.updateById(courseChapter));
	}


	/**
	 * 归属某个课程的具体章节列表 删除
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.DELETE,describe = "课程章节删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(courseChapterService.deleteLogic(Func.toLongList(ids)));
	}



	/**
	 * 章节下拉接口
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/select")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "章节下拉接口", notes = "传入courseId")
	public R courseSelect(Long courseId) {
		QueryWrapper<CourseChapterEntity> queryWrapper =new QueryWrapper<>();
		if (null!=courseId) queryWrapper.lambda().eq(CourseChapterEntity::getCourseId,courseId);
		return R.data(courseChapterService.list(queryWrapper));
	}

}
